package com.taxshare.manager.controller;

import com.taxshare.manager.service.LoginService;
import com.taxshare.manager.view.vo.LoginReqVO;
import com.taxshare.pvalidate.ParamValid;
import com.taxshare.response.beans.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName: LoginController
 * @Description: 用户登录
 * @author: jiangye
 * @date: 2021/2/4 9:41
 */
@RestController
@RequestMapping(value = "/login")
@Api(tags = "用户登录")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Value("${sso.uri}")
    private String ssoUri;

    @Value("${sso.service}")
    private String ssoService;

    @Value("${sso.protocol}")
    private String protocol;


    /**
     * 账号密码登录
     * @param loginBean
     * @param response
     * @return
     * @throws Exception
     */
    @ParamValid
    @PostMapping("/verify")
    @ApiOperation(value = "用户密码登录", notes = "用户密码登录")
    public Response verify(@RequestBody @Valid LoginReqVO loginBean, HttpServletResponse response) throws Exception {
        return loginService.verify(loginBean ,response);
    }

    /**
     * 刷新Token
     */
    @PostMapping("/refresh")
    @ApiOperation(value = "刷新Token", notes = "刷新Token")
    public Response refresh(String oldToken) throws Exception {
        return loginService.refresh(oldToken);
    }

    /**
     * 根据用户id获取token
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getToken")
    @ApiOperation(notes = "用户信息", value = "根据用户id获取token")
    public Response getToken(@RequestParam String userid) throws Exception {
        return loginService.getToken(userid);
    }

    /**
     * 获取sso的跳转地址
     *
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getSSOUrl")
    @ApiOperation(notes = "获取ssoUrl", value = "获取ssoUrl")
    public Response getSSOUrl(){
        String url=protocol+"://"+ssoUri+"/login?service="+ssoService;
        return Response.success(url);
    }

    /**
     * sso登录
     * @param ticket
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.POST, value = "/ssoLogin")
    @ApiOperation(notes = "sso登录", value = "sso登录")
    public Response ssoLogin(String ticket, String service, String uri) throws Exception{
        return loginService.ssoLogin(ticket,service,ssoUri);
    }

    /**
     * sso登录
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.POST, value = "/ssoCarLogin")
    @ApiOperation(notes = "ssoCar登录", value = "ssoCar登录")
    public Response ssoCarLogin(String token) throws Exception{
        return loginService.ssoCarLogin(token,ssoUri);
    }
}
